<template>
  <el-dialog
    :title="type===1?'更改设备':type===2?'换绑设备':'绑定设备'"
    width="50%"
    :visible.sync="visible"
    @close="$emit('update:show', false)"
    :show="show"
  >
    <el-form  :model="formInline"  label-width="80px" label-position="left" :rules="rules" ref="updateInfors" >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备编号" prop="trackerId">
            <el-input v-model="formInline.trackerId" placeholder="设备编号" disabled ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="激活时间" prop="bindTime">
            <el-input v-model="formInline.bindTime" placeholder="激活时间" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"  v-if="type===1">
          <el-form-item label="手机号" prop="telephone">
            <el-input v-model="formInline.telephone" placeholder="手机号" :disabled="type===1?true:false" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车牌号" prop="number">
            <el-input v-model="formInline.number" placeholder="车牌号" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用人" prop="userName">
            <el-input v-model="formInline.userName" placeholder="使用人"  ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品牌" prop="brand">
            <el-input v-model="formInline.brand" placeholder="品牌"  ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="颜色" prop="color">
            <el-input v-model="formInline.color" placeholder="颜色" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="type===2">
          <el-form-item label="旧手机号" prop="preTelephone">
            <el-input v-model="formInline.preTelephone" placeholder="旧手机号" disabled ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="type===2">
          <el-form-item label="新手机号" prop="newtelephone">
            <el-input v-model="formInline.newtelephone" placeholder="新手机号" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="type===3">
          <el-form-item label="手机号" prop="telephone">
            <el-input v-model="formInline.telephone" placeholder="手机号" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="close('updateInfors')">取 消</el-button>
    <el-button type="primary" @click="sure('updateInfors')">确 定</el-button>
  </span>
  </el-dialog>
</template>

<script>
  /**
   * 1.更改设备
   * 2.换绑设备
   * 3.绑定设备
   * **/
  import ShowMadal from '@/mixins/showmadal'
  import {validatePhone} from '@/utils/validate'
  export default {
    name: 'Modal',
    mixins: [ShowMadal],
    props: {
      row: {
        type: Object,
        default: {}
      },
      type: {
        type: Number,
        default: 1
      }
    },
    watch: {
      row: function() {
        this.formInline = {
          trackerId:this.row.trackerId,
          bindTime:this.row.bindTimeStr || '',
          userName:this.row.userName || '',
          brand:this.row.brand || '',
          number:this.row.number ||'',
          color:this.row.color ||'',
          preTelephone:this.row.telephone ||'',
          telephone:this.row.telephone,
          newtelephone:''
        }
      }
    },
    data: function() {
      var checkPhone = (rule, value, callback) => {
        if (!validatePhone(value)) {
          return callback(new Error('请填写正确的手机号'))
        }else {
          callback()
        }
      }
      return {
        formInline: {},
        rules: {
          telephone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { type: 'number', validator: checkPhone, trigger: 'blur' }
          ],
          newtelephone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { type: 'number', validator: checkPhone, trigger: 'blur' }
          ]
        }
      }
    },
    methods:{
      sure(formName) {
        this.$emit('submit', {formName: this.$refs[formName],formInline:this.formInline })
      },
      close(formName) {
        this.$emit('close', this.$refs[formName])
      }
    }
  }
</script>

<style scoped>

</style>
